unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Mask, RzEdit, ExtCtrls, RzPanel;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit_Subj: TRzEdit;
    Edit_RgEx: TRzEdit;
    RzToolbar2: TRzToolbar;
    RzToolbar1: TRzToolbar;
    Button2: TButton;
    Btnx_1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Btnx_1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  PerlRegEx;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  PerlA:TPerlRegEx;
begin
  //[4]=[T1].[1](D1)+[T1].[2](D3,D7)-[T2].[4](D5,D7)
  //\[\d+\]=\[T\d+\].\[\d+\](\(D\d+(,D\d+)*\)|\(D\d+:D\d+\))((\+|\-)(\[T\d+\].\[\d+\](\(D\d+(,D\d+)*\)|\(D\d+:D\d+\))))*
  //\[\d+\]=\[T\d+\].\[\d+\](\(D\d+(,D\d+)*\)|\(D\d+:D\d+\))((\+|\-)(\[T\d+\].\[\d+\](\(D\d+(,D\d+)*\)|\(D\d+:D\d+\))))*
  //\[\d+\]=\[T\d+\].\[\d+\](\(D\d+(,D\d+)*\)|\(D\d+:D\d+\))*((\+|\-)(\[T\d+\].\[\d+\](\(D\d+(,D\d+)*\)|\(D\d+:D\d+\))*))*
  //\[\d+\]=\[T\d+\].\[\d+\](\(D\d+(,D\d+)*\)|\(D\d+:D\d+\))*((\+|\-)(\[T\d+\].\[\d+\](\(D\d+(,D\d+)*\)|\(D\d+:D\d+\))*))*
  //\[\d+\]=\[T\d+\].\[\d+\](\((,?D\d+:D\d+|,?D\d+)*\))*((\+|\-)(\[T\d+\].\[\d+\](\((,?D\d+:D\d+|,?D\d+)*\))*
  //\[\d+\]=\[T\d+\].\[\d+\](\((,?D\d+:D\d+|,?D\d+)*\))*((\+|\-)(\[T\d+\].\[\d+\](\((,?D\d+:D\d+|,?D\d+)*\))*))*  
  //(D\d+:D\d+|,D\d+:D\d+|,D\d+|D\d+)*
  //(\((,?D\d+:D\d+|,?D\d+)*\))*
  try
    //PerlA:=TPerlRegEx.Create;
    PerlA:=TPerlRegEx.Create;
    //PerlA.RegEx:='\[\d+\]=\[T\d+\].\[\d+\]((\+|\-)(\[T\d+\].\[\d+\]))*';
    //PerlA.RegEx:='\[\d+\]=\[T\d+\].\[\d+\]\(\)';
    //PerlA.Subject:='[4]=[T1].[1]+[T1].[2]-[T2].[4]';
    //(\(D\d+(,D\d+)*\)|\(D\d+:D\d+\))
    PerlA.RegEx  :=Trim(Edit_RgEx.Text);
    PerlA.Subject:=Trim(Edit_Subj.Text);
    while PerlA.MatchAgain do
    begin
      //ShowMessage(PerlA.MatchedText);
      ShowMessage(PerlA.MatchedText);
    end;  
    {if PerlA.Match then
    begin
      ShowMessage('true');
    end;}
  finally
    FreeAndNil(PerlA);
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  I,M:Integer;
  PerlA:TPerlRegEx;
  TempA:string;
  StrsA:TStringList;
  NumbA:Integer;
  NumbB:Integer;
  TempB:string;
  IdexA:Integer;
begin
  StrsA:=TStringList.Create;
  TempB:='';
  try
    //PerlA:=TPerlRegEx.Create;
    PerlA:=TPerlRegEx.Create;    
    PerlA.RegEx:='\d+';
    PerlA.Subject:='(D1,D2,D3:D7)';
    while PerlA.MatchAgain do
    begin
      Writeln(PerlA.MatchedText);
      Writeln('xxxxxxxxxxxxxxxxxxxxx');
      Writeln(inttostr(PerlA.MatchedOffset));
      if StrsA.IndexOf(PerlA.MatchedText)=-1 then
      begin
        StrsA.Add(PerlA.MatchedText);
      end;
      
      TempA:=Copy(PerlA.Subject,PerlA.MatchedOffset+length(PerlA.MatchedText),1);
      Writeln(TempA);

      if TempA=')' then
      begin
        TempB:=TempB+PerlA.MatchedText;
      end else
      begin
        TempB:=TempB+PerlA.MatchedText+TempA;
      end;  

      Writeln('tempb:'+TempB);

      Writeln('aaaaaaaaaaaa');
      Writeln(strsa.text);
    end;
      Writeln('bbbbbbbbbbbb');
    StrsA.Clear;
    StrsA.DelimitedText:=TempB;
    StrsA.Delimiter:=',';
    Writeln(strsA.text);

    for I:= StrsA.Count-1 downto 0 do
    begin
      IdexA:=Pos(':',StrsA.Strings[I]);
      if IdexA>0 then
      begin
        NumbA:=StrToInt(Copy(StrsA.Strings[I],1,IdexA-1));
        NumbB:=StrToInt(Copy(StrsA.Strings[I],IdexA+1,MaxInt));

        Writeln(inttostr(numba));
        Writeln(inttostr(NumbB));

        for M:= NumbA to NumbB do
        begin
          StrsA.Add(IntToStr(M));
        end;
        StrsA.Delete(I);     
      end;
    end;
  finally
    FreeAndNil(PerlA);
  end;

  Writeln(StrsA.Text);

  //(\+|\-)?\(?((\+|\-)?(\[\d+\]\:\[\d+\]|\[\d+\]))*\)?
  //(\+|\-)?\(?((\+|\-)?(\[\d+\]\:\[\d+\]|\[\d+\]))+\)?
  //\[\d+\]=((\+|\-)?\(?((\+|\-)?(\[\d+\]\:\[\d+\]|\[\d+\]))+\)?)*
  //\[\d+\]=((\+|\-)?\(?((\+|\-)?(\[\d+\]\:\[\d+\]|\[\d+\]))+\)?)*    
end;

procedure TForm1.Btnx_1Click(Sender: TObject);
begin
  Application.Terminate;
end;

end.
